package org.jetnuke.modules.admin.stories;

import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.entities.Stories;
import org.jetnuke.util.JetUtil;

public class UpdateStory implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(UpdateStory.class);

	int id;
	String subject, notes, story, story_ext;
	int topic;
	int cat;

	public void setNotes(String notes) {
		this.notes = notes;
	}

	public void setStory(String story) {
		this.story = story;
	}

	public void setStory_ext(String story_ext) {
		this.story_ext = story_ext;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public void setId(int id) {
		this.id = id;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.common.IAction#exec(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse,
	 *      org.apache.velocity.VelocityContext, org.jetnuke.common.IModuleInfo,
	 *      java.lang.String, org.jetnuke.common.IUserInfo,
	 *      org.jetnuke.common.IAdminInfo)
	 */
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		if (ai == null || ai.isSuperUser() == false)
			return null;
		Stories stories = DataAccessDriver.getInst().newStoriesDAO()
				.retrieveByPK(id);
		stories.setHOMETEXT(story);
		stories.setBODYTEXT(story_ext);
		stories.setNOTES(notes);
		stories.setCATID(cat);
		stories.setTOPIC(topic);

		DataAccessDriver.getInst().newStoriesDAO().update(stories);
		JetUtil.redirect(vc, response, request.getContextPath()
				+ "/do/news/story?id=" + id);

		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return null;
	}

	/**
	 * @param cat
	 *            The cat to set.
	 */
	public void setCat(int cat) {
		this.cat = cat;
	}

	/**
	 * @param topic
	 *            The topic to set.
	 */
	public void setTopic(int topic) {
		this.topic = topic;
	}
}
